/*
题目链接:https://leetcode.cn/problems/count-good-triplets/?envType=daily-question&envId=2025-04-14
*/
//题解代码:
class Solution {
public:
    int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
        int n = arr.size(), mx = ranges::max(arr);
        int ans = 0;
        vector<int> s(mx+2);
        for(int j=0;j<n;++j){
            int y = arr[j];
            for(int k=j+1;k<n;++k){
                int z = arr[k];
                if(abs(z-y) > b){
                    continue;
                }
                int l = max({y-a, z-c, 0});
                int r = min({y+a, z+c, mx});
                ans += max(s[r+1]-s[l], 0);
            }
            for(int v=y+1;v<mx+2;++v) s[v]++;
        }
        return ans;
    }
};
